<template>
  <ElDrawer v-model="opened" :title="formType" size="40%" destroy-on-close>
    <ElSkeleton v-if="loading" animated :rows="10" />
    <ElForm v-else :model="form" :rules="rules" ref="formRef" status-icon :disabled="isView">
      <ElFormItem label="上级分类" :label-width="labelWidth" prop="parentId">
        <el-tree-select
          v-model="form.parentId"
          placeholder="选择上级菜单"
          :data="parentOptions"
          filterable
          check-strictly
          :disabled="isView"
          :render-after-expand="false" />
      </ElFormItem>
      <ElFormItem label="分类名称" :label-width="labelWidth" prop="categoryName">
        <ElInput v-model="form.categoryName" />
      </ElFormItem>
      <!-- <ElFormItem label="图层名称" :label-width="labelWidth" prop="layerCode">
        <ElInput v-model="form.layerCode" />
      </ElFormItem> -->
      <ElFormItem label="概况使用" :label-width="labelWidth" prop="useOverview">
        <el-select v-model="form.useOverview" :clearable="true" :fit-input-width="true">
          <el-option key="0" label="禁止" :value="0" />
          <el-option key="1" label="允许" :value="1" />
        </el-select>
      </ElFormItem>
    </ElForm>
    <template #footer>
      <ElButton v-if="!isView" type="primary" @click="onSaveClicked">保存</ElButton>
      <ElButton @click="onCancelClicked">取消</ElButton>
    </template>
  </ElDrawer>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted, unref, computed } from 'vue'
import { ElForm, ElFormItem, FormRules, FormInstance } from 'element-plus'
import {
  TbySpeciesCategory,
  getSpeciesCategoryDetail,
  saveOrUpdateSpeciesCategory,
  optionsSpeciesCategory
} from '@api/tby/common/speciesCategory'
import { FormType } from '@/enums'
const labelWidth = 100
const opened = ref(false)
const loading = ref(true)
const formType = ref<FormType>()
const formRef = ref<FormInstance>()
const form = ref<Partial<TbySpeciesCategory.Form>>({})
const parentOptions = ref<OptionType[]>([])

onMounted(async () => {
  console.log('进来了')
})

// 是否查看
const isView = computed(() => {
  return formType.value === FormType.VIEW
})

// 是否新增
const isAdd = computed(() => {
  return formType.value === FormType.ADD
})

const rules = reactive<FormRules>({
  categoryName: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
    { max: 20, message: '分类名称最大20个字符', trigger: 'blur' }
  ]
})

const open = async (type: FormType, parentId: number, id?: number) => {
  form.value = {}
  formType.value = type
  opened.value = true
  const { data } = await optionsSpeciesCategory()
  parentOptions.value = [{ value: 0, label: '顶级分类', children: data }]
  formRef.value?.clearValidate()
  if (isAdd.value) {
    loading.value = false
    form.value.parentId = parentId
    return
  }
  try {
    const { data } = await getSpeciesCategoryDetail(id!)
    loading.value = false
    form.value = data
  } finally {
    loading.value = false
  }
}

function close() {
  loading.value = true
  opened.value = false
  form.value = {}
}

const onSaveClicked = () => {
  if (!formRef.value) return
  formRef.value?.validate(valid => {
    if (!valid) {
      return
    }
    const formData = { ...unref(form) }
    saveOrUpdateSpeciesCategory(formData).then(() => {
      sendSuccess()
      close()
    })
  })
}

const onCancelClicked = () => {
  close()
}

defineExpose({
  open,
  close
})

const emit = defineEmits(['success'])

function sendSuccess() {
  emit('success')
}
</script>
